﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using DMC.Database;
using System.Linq;

namespace prjBaoCaoThamDinh
{
    public partial class frmBaoCaoThamDinh : Form
    {
        clsDatabase clsdata;
        ThongTinThamDinh clsthongtin;
        DataTable dtTuDien;
        DataTable dtDVHC;
        public string strconnect = "";
        public string strMaDVH="";
        public string Connect 
        {
            get { return strconnect; }
            set { strconnect = value; }
        }
        public frmBaoCaoThamDinh(string vconnection,string vMaDonViHanhChinh)
        {            
            clsdata = new clsDatabase();
            dtTuDien = new DataTable();
            dtDVHC = new DataTable();
            //clsdata.Connection = strconnect;
            //"SERVER=.;DATABASE=TayHo_full ;UID=sa ;PWD=123456";
            clsdata.Connection = vconnection;
            strMaDVH = vMaDonViHanhChinh;
            clsdata.OpenConnection();
            InitializeComponent();
            LoadcbbTuDienThamDinh();
            LoadcbbDVHC();
           
        }
        private void DinhDangRPV()
        {
            rpvBaoCaoKetQuaThamDinh.SetDisplayMode(DisplayMode.PrintLayout);
            rpvBaoCaoKetQuaThamDinh.ZoomMode = ZoomMode.Percent;
            rpvBaoCaoKetQuaThamDinh.ZoomPercent = 100;
        }
        public void LoadcbbTuDienThamDinh()
        {
            string[] para = { };
            string[] values = { };  
            clsdata.getValue(ref dtTuDien, "CT_SelectTuDienThamDinh", para, values);
            cmbKetQuaDKND.DataSource = dtTuDien;
            cmbKetQuaDKND.DisplayMember = "MoTa";
            cmbKetQuaDKND.ValueMember="KyHieu";
        }
        public void LoadcbbDVHC()
        {
            string[] para = { "@MaDVHC"};
            string[] values = { strMaDVH };
            //clsdata.getValue(ref dtDVHC, "CT_SelectDonViHanhChinh", para, values);
            clsdata.getValue(ref dtDVHC, "CT_SelectDonViHanhChinh", para, values);
            cmbDonViHanhChinh.DataSource = dtDVHC;
            cmbDonViHanhChinh.DisplayMember = "Ten";
            cmbDonViHanhChinh.ValueMember = "MaDonViHanhChinh";
        }       
        private void LocTheoNgayThamDinh(ref DataTable dt)
        {            
            if (dtpTuNgay.Checked == true)
            {
                DataTable dttmp = new DataTable();
                DataRow[] drs = dt.Select("NgayThamDinhDKND >=#" + dtpTuNgay.Value.ToShortDateString() + "# and NgayThamDinhDKND<= #" + dtpDenNgay.Value.ToShortDateString() + "#");
                dttmp = dt.Clone();
                if (drs.Length > 0)
                {
                    foreach (DataRow dr in drs)
                    {
                        dttmp.ImportRow(dr);
                    }
                }
                dt = new DataTable();
                dt = dttmp;
            }
            //return dt;
        }
        private string LayTenQuanByMaDonViHanhChinh()
        {
            DataTable dt= new DataTable();
            string[] param = { "@MaDVHC","@flag" };
            string[] values = { cmbDonViHanhChinh.SelectedValue.ToString(),0.ToString()};           
            clsdata.getValue(ref dt, "CT_SelectTenQuanByMaDonViHanhChinh", param, values);
            if (dt.Rows.Count > 0) return dt.Rows[0][0].ToString();
            else return "";
        }       
        private void LoadRptThamDinh()
        {
            List<ThongTinThamDinh> list = new List<ThongTinThamDinh>();
            DataTable dt = new DataTable();
            string[] para = { "@KetQuaThamDinhDKND", "@MaDonViHanhChinh" };
            string[] values = {cmbKetQuaDKND.SelectedValue.ToString(),cmbDonViHanhChinh.SelectedValue.ToString()};
            clsdata.getValue(ref dt, "CT_SelectBaoCaoThamDinhbyKetQuaThamDinhDKND", para, values);

            if (dtpTuNgay.Checked == true) LocTheoNgayThamDinh(ref dt);
            //if (chkKetQuaThamDinh.Checked == true && cmbKetQuaDKND.DataSource != null) LocTheoKetQuaThamDinh(ref dt);
            //if (chkDonViHanhChinh.Checked == true && cmbDonViHanhChinh.DataSource != null) LocTheoDonViHanhChinh(ref dt);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                clsthongtin = new ThongTinThamDinh();
                clsthongtin.HoTen = dt.Rows[i]["HoTen"].ToString();
                clsthongtin.ToBanDo = dt.Rows[i]["ToBanDo"].ToString();
                clsthongtin.SoThua = dt.Rows[i]["SoThua"].ToString();
                clsthongtin.DienTich =dt.Rows[i]["DienTich"].ToString();
                clsthongtin.MucDichSuDung = dt.Rows[i]["MucDichSuDung"].ToString();
                //MessageBox.Show(dt.Rows[i]["NgayThamDinhDKND"].ToString());
                clsthongtin.NguonGocVaThoiDiemBatDauSuDung = dt.Rows[i]["NguonGocVaThoiDiemBatDauSuDung"].ToString();
                clsthongtin.NgayThamDinhDKND = DateTime.Parse(dt.Rows[i]["NgayThamDinhDKND"].ToString()).ToShortDateString();                
                list.Add(clsthongtin);
            }
            List<ReportParameter> param = new List<ReportParameter>();
            param.Add(new ReportParameter("TenPhuong",cmbDonViHanhChinh.Text));
            param.Add(new ReportParameter("TenPhuongHoa", cmbDonViHanhChinh.Text.ToUpper()));
            param.Add(new ReportParameter("TenQuan",LayTenQuanByMaDonViHanhChinh()));
            param.Add(new ReportParameter("KetQuaThamDinh",cmbKetQuaDKND.Text.ToLower()));
            string tungay = "";
            string denngay = "";
            if (dtpTuNgay.Checked == true)
            {
                tungay = dtpTuNgay.Value.ToShortDateString();
                denngay = dtpDenNgay.Value.ToShortDateString();
            }
            //param.Add(new ReportParameter("TuNgay", tungay));
           // param.Add(new ReportParameter("DenNgay", denngay));           
            
            rpvBaoCaoKetQuaThamDinh.LocalReport.SetParameters(param);

            this.rptThongTinThamDinh.DataSource = list;
            this.rpvBaoCaoKetQuaThamDinh.RefreshReport();
        }
        private void Form1_Load(object sender, EventArgs e)
        {            
            //this.reportViewer1.RefreshReport();
        }
        private void dtpNgayThamDinhDKND_ValueChanged(object sender, EventArgs e)
        {
            if (dtpTuNgay.Checked == false) dtpDenNgay.Checked = false;
            else dtpDenNgay.Checked = true;
        }
        private void btnBaoCaoThongKe_Click(object sender, EventArgs e)
        {
            try
            {
                LoadRptThamDinh();
                DinhDangRPV();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }       
    }
}
